
import os
#import base64

def main():
    HASH_TYPE = "md5"

##    if HASH_TYPE == "md5":
##    #                print "md5"
##                    m = hashlib.md5()
##    else:
##     #               print "sha1"
##                    m = hashlib.sha1()

    ##if(HASH_TYPE == "sha1"):
    ##        m.update('The quick brown fox jumps over the lazy dog')
    ##        a = '2fd4e1c67a2d28fced849ee1bb76e7391b93eb12'
    ##        b = m.hexdigest()
    ##        print a == b



    ##teststr = '1e491ab1fd2d2a2219cd2cbe92a79c5e'
    ##smartgit = '1ede38c87812c0fd2a23592f09313d84'
    ##msysgit = '04d0fa6881dab7cfeea5d83121003d9c4971e417'
    ##gitex = '7974a8af376846d6b193b6bd57f948e2d7c76bd6'

    checksum = raw_input("enter the " + HASH_TYPE + " value: ")
    #print "choose the file for " + HASH_TYPE + "checksum: "

    #print testFile
    count = 0
    while(True):
        testFile = fileChoose('rb')
        if(testFile != None):
            print (fileHashCheck(testFile,HASH_TYPE) == checksum) # will produce what you're looking for
            break
        elif(count<1):
            print "No file Choosen, Choose a file: "
            count = count+1
        else:
            break
    sys.stdin.read(2)
    

def fileChoose(readMode):
    import Tkinter
    import tkFileDialog
    t = Tkinter.Tk()
    #t.focus()
    t.withdraw()  #hides the tk root window
    #parent=root,mode='rb',title='Choose a file'
    
    testFile = tkFileDialog.askopenfile(parent=None,mode=readMode,title='Choose a file to calculate checksum')
    #print checksum
    #filename = "J:/smartgit-win32-setup-jre-2_1_7.zip"
    #testFile = open(filename, "rb")
    #print testFile
    return testFile



def fileHashCheck(testFile, HASH_TYPE):
    import hashlib
    print "fileHashCheck"
    if(HASH_TYPE == "md5"):
                hash = hashlib.md5()
    else:
                hash = hashlib.sha1()

    while True:
        piece = testFile.read(512)

        if piece:
                hash.update(piece)
        else: # we're at end of file
            #change by shibli
            hash.update(piece)
            hex_hash = hash.hexdigest()
            break
    return hex_hash.lower()





if __name__ == '__main__':
    main()




